package Tasks;

import java.util.Random;

import Plants.PowerPlant;
import SmartGrid.ConsumerEnergyMeter;
import SmartGrid.EnergyMarket;
import SmartGrid.SmartGrid;

/**
 * This task is responsible for randomly adding and removing customers, and manipulating their energy usage throughout the day
 * @author jeffrey
 *
 */
public class ManipulateDemand extends MyRunnable{
	
	Random rand;
	
	public ManipulateDemand()
	{
		rand = new Random();
	}
	
	@Override
	public synchronized void run() {
		if(super.haltExecution()) return;
		double newCustomer = rand.nextDouble();
		if(newCustomer >= 0.5)
		{
			/*
			 * Add a consumer to a random plant
			 */
			PowerPlant plant = SmartGrid.getRandomPlant();
			ConsumerEnergyMeter consumer = new ConsumerEnergyMeter(plant);
			plant.addConsumer(consumer);
			consumer.setLocation(plant);
			EnergyMarket.demand.getAndAdd(consumer.getEnergyDemand());
		}
		else
		{
			/*
			 * Modify an existing consumer's energy demand
			 */
			ConsumerEnergyMeter consumer = SmartGrid.getRandomConsumer();
			int delta = consumer.changeDemand();
			EnergyMarket.demand.getAndAdd(delta);			
		}
		
		System.out.println("Energy demand: " + EnergyMarket.demand.get());
		System.out.println("Energy supply: " + EnergyMarket.supply.get());
	}

	
	
}
